package com.cskaoyan.util;

import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringToArrayUtils {
    //   []
    public static List<Integer> toArray(String str) {
        List<Integer> list = new ArrayList<>();
        String replace = str.replace("[", "");
        String numStr = replace.replace("]", "");
        if (StringUtils.isEmpty(numStr)) {
            return list;  // []
        }
        String[] split = numStr.split(",");
        if (split.length > 0) {
            for (String s : split) {
                int num = -1;
                try {
                    num = Integer.parseInt(s.trim());
                } catch (ArithmeticException e) {
                    System.out.println(s);
                }
                list.add(num);
            }
        }
        return list;
    }

    /**
     * WANGJun String2StringArr已测试
     *
     * @param str
     * @param clazz 返回列表的类型，仅支持基本数据类型
     * @param <T>
     * @return
     */
    public static <T> List<T> toArray(String str, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        String tString = str.substring(1, str.length() - 1);
        if (StringUtils.isEmpty(tString)) {
            return list;  // []
        }
        String[] tSplitArr = tString.split(", ");
        for (String s : tSplitArr) {
            if (s.matches("\".*\"")) {
                s = s.substring(1, s.length() - 1);

            }
            list.add((T) s);
        }
        return list;
    }

    public static List<String> toStringList(String gallery) {
        String s1 = gallery.replaceAll("\"", "");
        String s2 = s1.replaceAll(" ", "");
        String replace = s2.replace("[", "");
        String replace1 = replace.replace("]", "");
        String[] split = replace1.split(",");
        return new ArrayList<>(Arrays.asList(split));
    }
}
